<?php
/**
 * User: hey
 * Date: 2019/1/9
 * Time: 16:56
 */

namespace app;



use epii\admin\center\config\Settings;
use epii\admin\ui\lib\epiiadmin\jscmd\Alert;

use epii\admin\ui\lib\epiiadmin\jscmd\JsCmd;
use epii\admin\ui\lib\epiiadmin\jscmd\Refresh;
use epii\server\Args;
use libs\adminRole;
use model\AdminModel;
use model\RoleModel;
use service\admin\AdminService;
use service\admin\HospitalService;
use service\admin\RoleService;
use think\Db;

class admin_list extends base
{
    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 菜单
     */
    public function index()
    {
        $this->assign('roles', RoleService::getSelect());
        $this->adminUiDisplay('admin_list/index', "", ["version" => time()]);
    }

    /**
     * 表格数据
     */
    public function ajaxdata()
    {
        $map = [];
        $group_name = Args::postVal('role');
        if ($group_name) {
            $map["a.role"] = $group_name;
        }
        $name = Args::postVal('username');
        if ($name) {
            $map["a.username"] = $name;
        }
        echo AdminService::getAjaxData($map);
    }

    /**
     * @return array|false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 添加页面+添加
     */
    public function add()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            $id = Args::params("id/d");

            $username = trim(Args::params("username/1"));

            $password = Args::params("password" . ($id ? "" : "/1"));

            $group_name = trim(Args::params("group_name/1"));
            $status = trim(Args::params("status/1"));
            $role = trim(Args::params("role/1"));
            $hospital_id = trim(Args::params("hospital_id/d"));
            if (!$username || !$group_name || !$status || !$role) {
                $cmd = Alert::make()->msg('不能为空')->icon('5')->onOk(null);
                return JsCmd::make()->addCmd($cmd)->run();
            }

            if (!preg_match("/^[a-zA-Z]{1}[a-zA-Z\d_]{2,19}$/", $username)) {
                $this->error('用户名格式错误,必须以字母开头,只能包含字母、数字、下划线');
            }
            if ($password)
                if (!preg_match("/^[a-zA-Z\d_]{6,16}$/", $password)) {
                    $this->error('密码6~16位');
                }


            if (!preg_match("/^[\x{4e00}-\x{9fa5}]{2,8}$/u", $group_name)) {
                $this->error('昵称为2~8个汉字');
            }


            $data = [];

            $data['username'] = $username;
            if ($password)
            $data['password'] = md5($password);
            $data['group_name'] = $group_name;
            $data['status'] = $status;
            $data['role'] = $role;
            if($hospital_id){
                $data['hospital_id']=$hospital_id;
            }
            $data['updatetime'] = time();

            if (!$id) {
                $has = AdminModel::getInfo(['username'=>$username]);
                if ($has) {
                    $this->error('账号已存在');
                }
                $data['addtime']=time();
                $res = AdminService::add($data);
            } else {
                $res = AdminService::edit($id,$data);
            }
             $res['code']?$this->success($res['msg']):$this->error($res['msg']);

        } else {
            if ($id = Args::params("id/d")) {
                $this->_as_admin = AdminModel::getInfo(['id'=>$id]);
            }

            $roles = RoleModel::getList('','id,name');
            $this->assign('roles', $roles);
            $this->assign("status_arr", [["value" => "normal", "text" => "正常"], ["value" => "locked", "text" => "锁定"]]);
            $this->assign('hospitals',HospitalService::getSelect(['status'=>1]));
            $this->adminUiDisplay('admin_list/add');
        }
    }


    /**
     * @return array|false|string
     * @throws \think\Exception
     * @throws \think\db\exception\PDOException
     * 删除
     */
    public function del()
    {
        $id = Args::params('id');
        $res =AdminService::del($id);
        if ($res['code']) {
            Settings::_saveCache();
            $this->success();
        } else {
            $this->error();
        }
    }

}